#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/16 16:51
# @File    : aa.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

import tkinter as tk
from tkinter import ttk


def on_tree_select(event):
    # 获取选中项的信息
    item = tree.selection()[0]  # 假设只有一个选中项
    print(f"Selected item: {item}")

    # 获取自己的子集
    print("tree 子集:", tree.get_children())

    # 获取item的子集
    print(f"{item} 子集:", tree.get_children(item))

    # 获取子集数据 可以通过tree.item(item)获取更多详细信息
    item_data = tree.item(item)
    print(f"item{type(item_data)}:{item_data}")



def create_tree(root):
    tree = ttk.Treeview(root, show='tree')  # 使用'tree'来显示树形结构
    tree.pack(fill=tk.BOTH, expand=True)

    # 添加顶层节点
    top_item = tree.insert('', 'end', id=0, text='顶层节点1', tags="名称", open=True)
    print(type(top_item), top_item)
    tree.insert(top_item, 'end', id=1, text='子节点1')
    tree.insert(top_item, 'end', id=2, text='子节点2')

    # 添加另一个顶层节点
    top_item = tree.insert('', 'end', id=3, text='顶层节点2', open=True)
    tree.insert(top_item, 'end', id=4, text='子节点3')
    tree.insert(top_item, 'end', id=5, text='子节点4')

    grandson_item = tree.insert(top_item, 'end', id=6, text='子节点5')
    tree.insert(grandson_item, 'end', id=7, text='子节点6')
    tree.insert(grandson_item, 'end', id=8, text='子节点7')


    return tree


root = tk.Tk()
tree = create_tree(root)
tree.bind('<<TreeviewSelect>>', on_tree_select)
root.mainloop()